<?php

require_once "application.php";

class LoginController extends application {

    public function init() {

        parent::init();
        $this->_helper->layout->setLayout('layout');
    }

    public function indexAction() {
        $language = 'pl';
        $translator = new Zend_Translate(
                        'array',
                        '../application/resources/languages',
                        $language
        );
        Zend_Validate_Abstract::setDefaultTranslator($translator);
        require_once("../application/forms/Login.php");
        $form = new Application_Form_Login();
        $form->setAction('/login/')
                ->setMethod('post');
        if ($this->_request->isPost() && $form->isValid($_POST)) {
            // pobieramy dane z formularza
            $data = $form->getValues();
            // pobieramy domyślny adapter bazy danych
            $db = Zend_Db_Table::getDefaultAdapter();
            // tworzymy instancję adaptera autoryzacji
            $authAdapter = new Zend_Auth_Adapter_DbTable($db, 'uzytkownicy', 'email', 'haslo');
            // wprowadzamy dane do adaptera
            $authAdapter->setIdentity($data['email']);
            $authAdapter->setCredential(md5($data['password']));
            // sprawdzamy, czy użytkownik jest aktywny
            $authAdapter->setCredentialTreatment("AND active = '1'");
            // autoryzacja
            $result = $authAdapter->authenticate();
            if ($result->isValid()) {
                // umieszczamy w sesji dane użytkownika
                $auth = Zend_Auth::getInstance();
                $storage = $auth->getStorage();
                $storage->write($authAdapter->getResultRowObject(array(
                            'id', 'email', 'imie', 'nazwisko', 'rola'
                        )));
                return $this->_redirect('/login/finish/');
            } else {
                $this->view->loginMessage = "Niepoprawny login lub hasło.";
            }
        }
        $this->view->form = $form;
    }

    public function finishAction() {

    }

    public function logoutAction() {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        return $this->_redirect('/');
    }

    public function resetHaslaAction() {
        $language = 'pl';
        $translator = new Zend_Translate(
                        'array',
                        '../application/resources/languages',
                        $language
        );
        Zend_Validate_Abstract::setDefaultTranslator($translator);
        $form = new Application_Form_ResetHasla();
        $form->setAction('/login/reset-hasla')
                ->setMethod('post');
        if ($this->_request->isPost() && $form->isValid($_POST)) {
            Zend_Loader::loadClass('Uzytkownik');
            $uzytkownik = new Uzytkownik();
            $pass = md5(uniqid(0));
            $pass = substr($pass, 0, 20);
            $result = $uzytkownik->noweHaslo($_POST['email'], md5($pass));
            if ($result) {
                Zend_Loader::loadClass('Zend_Mail');
                $config = array(
                    'ssl' => 'tls',
                    'port' => '25',
                    'auth' => 'plain',
                    'username' => 'spamik@ciekawa-reklama.pl',
                    'password' => 'spamuj123');

                $mail = new Zend_Mail('utf-8');
                $transport = new Zend_Mail_Transport_Smtp('ainet.com.pl', $config);

                Zend_Mail::setDefaultTransport($transport);

                $body = "Nowe hasło potrzebne do zalogowania się to: " . $pass . "<br />
                    Będzie je można zmienić po zalogowaniu w ustawieniach profilu."
                ;

                $mail->setBodyHtml($body);

                $mail->setFrom('kontakt@ainet.com.pl', 'Ciekawa-Reklama.pl')
                        ->addTo($_POST['email'])
                        ->setSubject("Przypomnij hasło")
                        ->send();
                return $this->_redirect('/login/finish-reset');
            }
        }
        $this->view->form = $form;
    }

    public function finishResetAction() {

    }

}
